package com.bh.weapp.controller.sysuser;

import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.domain.entity.SysUser;
import com.bh.common.core.page.TableDataInfo;
import com.bh.kysys.domain.JcSysOrganization;
import com.bh.kysys.service.IJcSysOrganizationService;
import com.bh.kysys.vo.JcSysOrganizationInfoVo;
import com.bh.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 *
 * @author ruo-yi
 */
@Api(tags = {"管理员用户信息接口"})
@RestController
@RequestMapping("/api/sysuser")
public class SysUserApiController extends BaseController {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private IJcSysOrganizationService jcSysOrganizationService;

    @ApiOperation("查询管理员用户")
    @PreAuthorize("@ss.hasPermi('api:system:user:list')")
    @PostMapping("/listByName/{userName}")
    public TableDataInfo list(@PathVariable("userName") String userName) {
        SysUser user = new SysUser();
        user.setUserName(userName);
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    @ApiOperation("管理员管理网点信息")
    @PreAuthorize("@ss.hasPermi('api:system:user:org')")
    @GetMapping("/managerOrg")
    public AjaxResult managerOrg() {
        JcSysOrganization sysOrganization = jcSysOrganizationService.selectJcSysOrganizationById(getOrgId());
        JcSysOrganizationInfoVo infoVo = new JcSysOrganizationInfoVo();
        infoVo.setDetaileAddress(sysOrganization.getAddress());
        infoVo.setAddress(sysOrganization.getAddress());
        infoVo.setContactperson(sysOrganization.getPrincipal());
        infoVo.setIphone(sysOrganization.getDescription());
        return AjaxResult.success(infoVo);
    }

    @ApiOperation("管理员管理网点信息")
    @PreAuthorize("@ss.hasPermi('api:system:user:org')")
    @PostMapping("/managerOrg")
    public AjaxResult managerOrg(@RequestBody JcSysOrganizationInfoVo jcSysOrganizationInfoVo) {
        JcSysOrganization sysOrganization = jcSysOrganizationService.selectJcSysOrganizationById(getOrgId());
        sysOrganization.setLatitude(jcSysOrganizationInfoVo.getLtl());
        sysOrganization.setAddress(jcSysOrganizationInfoVo.getDetaileAddress());
        sysOrganization.setPrincipal(jcSysOrganizationInfoVo.getContactperson());
        sysOrganization.setDescription(jcSysOrganizationInfoVo.getIphone());
        return AjaxResult.success(jcSysOrganizationService.updateJcSysOrganization(sysOrganization));
    }
}